package a2022.a20220602;

import java.util.Arrays;

/**
 * @author lenovo
 * @date 2022/6/2
 */
public class a {
    public static void main(String[] args) {
        int[] quantities = {11,6};
        minimizedMaximum(6,quantities);
    }
    public static int minimizedMaximum(int n, int[] quantities) {
        Arrays.sort(quantities);
        int left = 1;//设置左端点,因为你要是设置为0了就没有意义了,而且getSum函数也会报错
        int right = quantities[quantities.length-1]+1;//右端点为最大值加1,因为我们的右端点一直取不到,所以加1,再大的话就肯定分配不过来了,因为n>=m
        while(left < right){
            int mid = left + (right - left)/2;
            int num = getNum(mid,quantities);
            if(n >= num){//如果n>=num说明我们max=mid下分配的商家少了,那我们就应该将mid减小
                right = mid;
            }else if(n < num){//如果n<num说明我们max=mid下分配的商家多了,那我们就应该将mid增大
                left = mid + 1;
            }
        }
        return left;
    }
    private static int getNum(int mid, int[] quantities) {
        int count = 0;
        for (int quantity : quantities) {
            count += quantity / mid + (quantity % mid == 0 ? 0 : 1);
        }
        return count;
    }

}
